// serial.h
//
//
// updated by akkdong 2008-0824
//
//    add PRINT define
//
// updated by akkdong 2008-08-14
//
//    support ATTiny2313
//    support AT90S2313
//    add uart_puts_p()
//
// created by akkdong 2008-08-13
//
//    support ATMega128(not tested), ATMega8
//

#ifndef __SERIAL_H__
#define __SERIAL_H__


#if	defined(__AVR_ATmega128__)

#ifdef USE_UART_ONE

#define UART_DATA		UDR1
#define UART_STATUSA	UCSR1A
#define UART_STATUSB	UCSR1B
#define UART_STATUSC	UCSR1C
#define UART_BAUDRATEH	UBRR1H
#define UART_BAUDRATEL	UBRR1L

#else

#define UART_DATA		UDR0
#define UART_STATUSA	UCSR0A
#define UART_STATUSB	UCSR0B
#define UART_STATUSC	UCSR0C
#define UART_BAUDRATEH	UBRR0H
#define UART_BAUDRATEL	UBRR0L

#endif // USE_UART_ONE

#elif defined(__AVR_ATmega32__) || defined(__AVR_ATmega8__) || defined(__AVR_ATtiny2313__) || defined(__AVR_AT90S2313__)

#define UART_DATA		UDR
#define UART_STATUSA	UCSRA
#define UART_STATUSB	UCSRB
#define UART_STATUSC	UCSRC
#define UART_BAUDRATEH	UBRRH
#define UART_BAUDRATEL	UBRRL

#else

	#error "serial not defined device"

#endif

#define USE_STANDARD_IO


#define MAX_SIO_COUNT		32


#define UART_BAUD_SELECT(X) 	(u_int)(((F_CPU)+((X)*8L))/((X)*16L)-1)
//#define UART_BAUD_SELECT(X) 	(u_int)((F_CPU)/(16L*(X))-1)


#define PRINT(x)					\
	{								\
		static prog_char str[] = x;	\
		uart_puts_P(str);			\
	}								\


enum uart_databit_e		{ databit_5, databit_6, databit_7, databit_8, databit_9 };
enum uart_stopbit_e		{ stopbit_1, stopbit_2 };
enum uart_paritybit_e 	{ parity_none, parity_even, parity_odd };

/*
typedef enum uart_databit_e 	uart_databit;
typedef enum uart_stopbit_e		uart_stopbit;
typedef enum uar_paritybit_e	uart_paritybit;
*/

/*
********************************************************************************
Function Prototype Definition Part
********************************************************************************
*/
void	uart_init(u_int baud_rate);				// UART_BAUD_SELECT(baud_speed) -> baud_rate
void	uart_databit(u_char databit);			// 5, 6, 7, 8 9, data bits
void	uart_stopbit(u_char stopbit);			// 1-bit, 20bit
void	uart_paritybit(u_char parity);			// 0 : none, 1 : odd, other : even

u_int	uart_keyhit(void);						// Check if there is Receive data

int 	uart_putchar(char c);
int 	uart_getchar();
void	uart_puts(const char * str);			// Send a string to UART
void	uart_puts_P(const prog_char * str);		// Send a prog string to UART
int		uart_gets(char * str, int max_len, char password); // Get a string from UART
void	uart_flush_rx();						// Flush RX Buffer 


#endif // __SERIAL_H__

